package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.service.IStrategyService;
import cn.wolfcode.luowowo.acticle.service.IStrategyTagService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;


    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) throws LogicException {
        //目的地id
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();
        detail.setDest(dest);
        //作者id
        //文章的摘要(截取)
        String sum = detail.getStrategyContent().getContent();
        if(sum.length() < 200){
            detail.setSummary(sum);
        }else{
            detail.setSummary(sum.substring(0,200));
        }
        //是否是国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        detail.setIsabroad(toasts.get(0).getId() != 1);


        if(detail.getId() == null){
            //创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            //处理文章内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        }else{
            strategyDetailMapper.updateByPrimaryKey(detail);
            //处理文章内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        //处理标签
        //每次插入前清除原有的
        //List<StrategyTag> redisTags = tagsRedisService.getTagsList();
        strategyDetailMapper.deleteRelation(detail.getId());
        if(StringUtils.hasLength(tags)){
            String[] split = tags.split(",");
            for (String s : split) {
                StrategyTag tag = new StrategyTag();
                tag.setName(s);
                strategyTagService.saveOrUpdate(tag);
                //维护多对多关系
                strategyDetailMapper.insertRelation(detail.getId(),tag.getId());
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getViewnumTop3(Long destId) {
        return strategyDetailMapper.selectByViewnumTop3(destId);
    }

    @Override
    public void changeState(Long id, Long state) throws LogicException {
        strategyDetailMapper.updateStateById(id,state);
    }

    @Override
    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
        strategyContentMapper.deleteById(id);
    }

    @Override
    public void updateReplyNumByDetailId(Long detailId, int num) {
        strategyDetailMapper.updateReplyNumByDetailId(detailId,num);
    }

    @Override
    public void updateStatisByVo(StrategyStatisVO vo1) {
        strategyDetailMapper.updateStatisByVo(vo1);
    }

    @Override
    public StrategyDetail selectByStrategyId(Long strategyId) {
        return strategyDetailMapper.getByStrategyId(strategyId);
    }
}
